package ru.kop4en.diplom.camera;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Iterator;

//=============================================================================
public class CameraPanel extends JPanel implements ActionListener
{
	private static final long serialVersionUID = 1L;
	
	public CameraPanel()
	{
		setLayout(new BorderLayout());
		
		add(center_panel, BorderLayout.CENTER);		
		add(right_panel_, BorderLayout.EAST);
		add(left_panel_, BorderLayout.WEST);
		add(bottom_panel_, BorderLayout.SOUTH);
		
		center_panel.setLayout(grid_layout);
		right_panel_.addCamHandler(this);
		
		BufferedImage newImage = new BufferedImage(2, 2, BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2d = newImage.createGraphics();
		// do your painting on g2d
		g2d.dispose();
		// newImage is now ready for use		
	}

	//=============================================================================
	@Override
	public String getName()
	{
		return "Camera";
	}
	
	//=============================================================================
	private void addCamera(int camera_count)
	{
		Camera camera = new Camera(camera_count);
		if (!camera.found())
		{
			JOptionPane.showMessageDialog(this, "Camera nod found");
			return;			
		}
		
		if (grid_layout.getColumns() > grid_layout.getRows())
		{
			grid_layout.setRows(grid_layout.getRows() + 1);
		}
		else
		{
			grid_layout.setColumns(grid_layout.getColumns() + 1);
		}	
		
		center_panel.add(camera);		
		updateImage();
		new Thread(camera).start();
		camera_list.add(camera);
	}
	
	//=============================================================================
	void updateImage()
	{
		revalidate();
		Iterator<Camera> iter = camera_list.iterator();
		while (iter.hasNext())
		{
			iter.next().updateSize(true);		
		}
	}
	
	@Override
	//=============================================================================
	public void paint(Graphics g)
	{
		// TODO Auto-generated method stub
		super.paint(g);
		
	}
	
	@Override
	//=============================================================================
	public void actionPerformed(ActionEvent e)
	{
		String command = e.getActionCommand();
		if (command.equals("Add"))
		{			
			addCamera(camera_count++);			
		}
	}
	
	//=============================================================================
	public void addWindowListener(ActionListener mainWindow)
	{
		right_panel_.addNextHandler(mainWindow);
		left_panel_.addPrevHandler(mainWindow);		
	}
	
	// members ===============================================
	private RightCameraPanel right_panel_ = new RightCameraPanel();
	private BottomCameraPanel bottom_panel_ = new BottomCameraPanel();
	private LeftCameraPanel left_panel_ = new LeftCameraPanel();
	private JPanel center_panel = new JPanel();
	private int camera_count = 0;
	private ArrayList<Camera> camera_list = new ArrayList<Camera>();
	private GridLayout grid_layout = new GridLayout(1, 0);
}
